﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using laba2.Context;
using laba2.Entity;
using laba2.Repository.IRepository;
using laba2.Repository.RepositoryImpl;

namespace laba2.Services
{
   public class StudentService
   {
    /*   private IStudentRepository _studentRepository;
       private ITaskRepository _taskRepository;
       private IAchivementRepository _achivementRepository;

       public StudentService(IStudentRepository studentRepository, ITaskRepository taskRepository, IAchivementRepository achivementRepository)
       {
           
       }
       */

       public StudentService()
       {
       }

       public void AddStudent(string name, string address, string email, string phone)
       {
           using (var context=new Lab3DBContext())
           {
               context.Students.Add(new Student(name, address, email, phone));
               context.SaveChanges();
           }
      }

       public void AddPracticalTask(int taskId, DateTime date, int ratingForQuality, int ratingForDate,int studentId)
       {
           using (var context=new Lab3DBContext())
           {
               var achivements = (from achivement in context.Achivements
                                  where achivement.TaskId == taskId && achivement.StudentId == studentId
                                  select achivement).FirstOrDefault();
               if (achivements == null)
               {
                   context.Achivements.Add(new Achivement(date, ratingForQuality + ratingForDate, taskId, studentId));
               }
               else
               {
                   achivements.Rating = ratingForQuality + ratingForDate;
                   achivements.Date = date;
               }
               context.SaveChanges();
           }
       }

       public void AddBoundaryControl(int taskId, DateTime date, int rating, int studentId)
       {
           using (var context=new Lab3DBContext())
           {
               var achivements = (from achivement in context.Achivements
                                  where achivement.TaskId == taskId && achivement.StudentId == studentId
                                  select achivement).FirstOrDefault();
               if (achivements == null)
               {
                   context.Achivements.Add(new Achivement(date, rating, taskId, studentId));
               }
               else
               {
                   achivements.Rating = rating;
                   achivements.Date = date;
               }
               context.SaveChanges();
           }
       }

       public void AddFinalControl(int taskId , DateTime date, int rating, int studentId)
       {
           using (var context = new Lab3DBContext())
           {
               var achivements = (from achivement in context.Achivements
                                  where achivement.TaskId == taskId && achivement.StudentId == studentId
                                  select achivement).FirstOrDefault();
               if (achivements == null)
               {
                   context.Achivements.Add(new Achivement(date, rating, taskId, studentId));
               }
               else
               {
                   achivements.Rating = rating;
                   achivements.Date = date;   
               }
               context.SaveChanges();
           }
       }

       public List<Task> GetListDebt(int idStudent, int idCourse)
       {
           using (var context = new Lab3DBContext())
           {
               List<Task> listDebt = new List<Task>();
               var courseTasks = (from task in context.Tasks
                                  where task.Course.Id == idCourse
                                  select task).ToList();
               var studentAchivements = (from achivement in context.Achivements
                                         where achivement.Student.Id == idStudent && achivement.Task.Course.Id == idCourse
                                         select achivement).ToList();
               foreach (Task courseTask in courseTasks)
               {
                   if (!ContainsTaks(studentAchivements, courseTask))
                   {
                       listDebt.Add(courseTask);
                   }
               }
               foreach (Achivement achivement in studentAchivements)
               {
                   if (!achivement.isPass())
                   {
                       listDebt.Add(achivement.Task);
                   }
               }
               return listDebt;
               }
       } 

       public int GetCurrentRating(int idStudent, int idCourse)
       {
           using (var context=new Lab3DBContext())
           {
               var achivements = (from achivement in context.Achivements.Include("Task").Include("Task.Course")
                                 where achivement.StudentId == idStudent && achivement.Task.CourseId == idCourse
                                 select achivement).ToList();
               int rating = 0;
               if(GetListDebt(idStudent,idCourse).Count==0)
               {
                   foreach (var achivement in achivements)
                   {
                       rating += achivement.Rating;
                   }
               }
               return rating;
           }
       }

      public List<Student> GetAll(){
          using(var context=new Lab3DBContext())
          {
              return context.Students.ToList();
          }
       }
 
       public void AddPracticalTask()
       {
           
       }
       private bool ContainsTaks(List<Achivement> achivements, Task task )
       {
           foreach (Achivement achivement in achivements)
           {
               if (achivement.Task.Equals(task)) return true;
           }
           return false;
       }
   }
}
